package com.xd.hbsys.aws;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xd.hbsys.common.BaseService;


@Service
public class AWSService extends BaseService {


    @Resource(name = "hbJdbcTemplate")
    private JdbcTemplate hbJdbcTemplate;

    @Resource
    private AWSDao awsdao;

    public String queryNow() {
//		String str = "[{\"CITY_CODE\":\"410100\",\"WINDDIRECT\":\"285.0\",\"VISIBILITY\":\"10.900\",\"DEWTEMP\":\"14.0\",\"RELHUMIDITY\":\"56\",\"WINDVELOCITY\":\"0.9\",\"DRYBULTEMP\":\"23.3\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1001.1\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"410200\",\"WINDDIRECT\":\"194.0\",\"VISIBILITY\":\"6.800\",\"DEWTEMP\":\"14.4\",\"RELHUMIDITY\":\"55\",\"WINDVELOCITY\":\"0.9\",\"DRYBULTEMP\":\"24.0\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1005.7\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"410300\",\"WINDDIRECT\":\"19.0\",\"VISIBILITY\":\"11.400\",\"DEWTEMP\":\"8.7\",\"RELHUMIDITY\":\"41\",\"WINDVELOCITY\":\"3.0\",\"DRYBULTEMP\":\"22.6\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"976.6\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"410400\",\"WINDDIRECT\":\"225.0\",\"VISIBILITY\":\"12.100\",\"DEWTEMP\":\"19.3\",\"RELHUMIDITY\":\"82\",\"WINDVELOCITY\":\"1.2\",\"DRYBULTEMP\":\"22.5\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"997.9\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"410500\",\"WINDDIRECT\":\"240.0\",\"VISIBILITY\":\"13.300\",\"DEWTEMP\":\"5.0\",\"RELHUMIDITY\":\"34\",\"WINDVELOCITY\":\"2.4\",\"DRYBULTEMP\":\"21.5\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"992.1\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"410600\",\"WINDDIRECT\":\"316.0\",\"VISIBILITY\":\"16.100\",\"DEWTEMP\":\"11.6\",\"RELHUMIDITY\":\"62\",\"WINDVELOCITY\":\"1.2\",\"DRYBULTEMP\":\"19.0\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1004.8\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"410700\",\"WINDDIRECT\":\"192.0\",\"VISIBILITY\":\"7.900\",\"DEWTEMP\":\"10.4\",\"RELHUMIDITY\":\"48\",\"WINDVELOCITY\":\"0.5\",\"DRYBULTEMP\":\"21.9\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1005.9\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"410800\",\"WINDDIRECT\":\"285.0\",\"VISIBILITY\":\"11.500\",\"DEWTEMP\":\"7.9\",\"RELHUMIDITY\":\"33\",\"WINDVELOCITY\":\"1.1\",\"DRYBULTEMP\":\"25.3\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1000.9\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"410900\",\"WINDDIRECT\":\"78.0\",\"VISIBILITY\":\"3.600\",\"DEWTEMP\":\"11.0\",\"RELHUMIDITY\":\"52\",\"WINDVELOCITY\":\"1.3\",\"DRYBULTEMP\":\"21.2\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1007.8\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411000\",\"WINDDIRECT\":\"184.0\",\"VISIBILITY\":\"30.000\",\"DEWTEMP\":\"16.0\",\"RELHUMIDITY\":\"73\",\"WINDVELOCITY\":\"0.9\",\"DRYBULTEMP\":\"21.0\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1006.5\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411100\",\"WINDDIRECT\":\"168.0\",\"VISIBILITY\":\"9.900\",\"DEWTEMP\":\"17.1\",\"RELHUMIDITY\":\"71\",\"WINDVELOCITY\":\"0.7\",\"DRYBULTEMP\":\"22.6\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1007.8\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411200\",\"WINDDIRECT\":\"62.0\",\"VISIBILITY\":\"7.900\",\"DEWTEMP\":\"9.5\",\"RELHUMIDITY\":\"49\",\"WINDVELOCITY\":\"1.7\",\"DRYBULTEMP\":\"20.6\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"967.7\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411300\",\"WINDDIRECT\":\"226.0\",\"VISIBILITY\":\"5.400\",\"DEWTEMP\":\"18.1\",\"RELHUMIDITY\":\"88\",\"WINDVELOCITY\":\"1.1\",\"DRYBULTEMP\":\"20.1\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"994.4\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411400\",\"WINDDIRECT\":\"201.0\",\"VISIBILITY\":\"12.500\",\"DEWTEMP\":\"17.8\",\"RELHUMIDITY\":\"90\",\"WINDVELOCITY\":\"1.3\",\"DRYBULTEMP\":\"19.5\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1008.4\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411500\",\"WINDDIRECT\":\"187.0\",\"VISIBILITY\":\"10.400\",\"DEWTEMP\":\"20.2\",\"RELHUMIDITY\":\"99\",\"WINDVELOCITY\":\"0.5\",\"DRYBULTEMP\":\"20.4\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1002.2\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411600\",\"WINDDIRECT\":\"244.0\",\"VISIBILITY\":\"10.600\",\"DEWTEMP\":\"17.3\",\"RELHUMIDITY\":\"67\",\"WINDVELOCITY\":\"1.3\",\"DRYBULTEMP\":\"23.8\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1009.2\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411700\",\"WINDDIRECT\":\"198.0\",\"VISIBILITY\":\"6.200\",\"DEWTEMP\":\"16.1\",\"RELHUMIDITY\":\"88\",\"WINDVELOCITY\":\"0.7\",\"DRYBULTEMP\":\"18.1\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1002.6\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411800\",\"WINDDIRECT\":\"318.0\",\"VISIBILITY\":\"8.800\",\"DEWTEMP\":\"12.2\",\"RELHUMIDITY\":\"65\",\"WINDVELOCITY\":\"0.4\",\"DRYBULTEMP\":\"18.9\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"998.0\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"410181\",\"WINDDIRECT\":\"0.0\",\"VISIBILITY\":\"9.600\",\"DEWTEMP\":\"12.5\",\"RELHUMIDITY\":\"59\",\"WINDVELOCITY\":\"0.0\",\"DRYBULTEMP\":\"20.8\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"994.7\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"410225\",\"WINDDIRECT\":\"217.0\",\"VISIBILITY\":\"13.700\",\"DEWTEMP\":\"11.9\",\"RELHUMIDITY\":\"48\",\"WINDVELOCITY\":\"1.4\",\"DRYBULTEMP\":\"23.5\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1005.4\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"410482\",\"WINDDIRECT\":\"92.0\",\"VISIBILITY\":\"6.100\",\"DEWTEMP\":\"15.9\",\"RELHUMIDITY\":\"69\",\"WINDVELOCITY\":\"0.9\",\"DRYBULTEMP\":\"21.8\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"991.0\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"410526\",\"WINDDIRECT\":\"236.0\",\"VISIBILITY\":\"12.400\",\"DEWTEMP\":\"10.6\",\"RELHUMIDITY\":\"49\",\"WINDVELOCITY\":\"1.2\",\"DRYBULTEMP\":\"21.8\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1006.7\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"410728\",\"WINDDIRECT\":\"0.0\",\"VISIBILITY\":\"12.800\",\"DEWTEMP\":\"12.6\",\"RELHUMIDITY\":\"59\",\"WINDVELOCITY\":\"0.0\",\"DRYBULTEMP\":\"20.9\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1006.6\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411381\",\"WINDDIRECT\":\"0.0\",\"VISIBILITY\":\"2.400\",\"DEWTEMP\":\"18.8\",\"RELHUMIDITY\":\"97\",\"WINDVELOCITY\":\"0.0\",\"DRYBULTEMP\":\"19.3\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1001.5\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411481\",\"WINDDIRECT\":\"165.0\",\"VISIBILITY\":\"9.600\",\"DEWTEMP\":\"17.4\",\"RELHUMIDITY\":\"81\",\"WINDVELOCITY\":\"2.1\",\"DRYBULTEMP\":\"20.8\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1010.8\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411525\",\"WINDDIRECT\":\"105.0\",\"VISIBILITY\":\"5.700\",\"DEWTEMP\":\"17.5\",\"RELHUMIDITY\":\"93\",\"WINDVELOCITY\":\"0.7\",\"DRYBULTEMP\":\"18.7\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1010.0\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411628\",\"WINDDIRECT\":\"255.0\",\"VISIBILITY\":\"9.200\",\"DEWTEMP\":\"17.5\",\"RELHUMIDITY\":\"72\",\"WINDVELOCITY\":\"1.9\",\"DRYBULTEMP\":\"22.8\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1009.8\",\"PRECIPITATION\":\"0.0\"},{\"CITY_CODE\":\"411729\",\"WINDDIRECT\":\"0.0\",\"VISIBILITY\":\"7.300\",\"DEWTEMP\":\"18.8\",\"RELHUMIDITY\":\"95\",\"WINDVELOCITY\":\"0.0\",\"DRYBULTEMP\":\"19.6\",\"TIME_CODE\":\"2018-06-01 01:00:00\",\"STATIONPRESS\":\"1010.6\",\"PRECIPITATION\":\"0.0\"}]";
//		return str;

        JSONArray json = null;

        json = _getcity();
        return json.toJSONString();

    }

    public String queryVisibility(String time) {
        String sql = "SELECT visibility FROM city_aws_day WHERE DATE = '" + time + "' AND city_name = '郑州市'";
        List<Map<String, Object>> rows = hbJdbcTemplate.queryForList(sql);
        Iterator it = rows.iterator();
        JSONObject obj = new JSONObject();
        if (it.hasNext()) {
            Map<String, Object> r = (Map<String, Object>) it.next();
            float VISIBILITY = (float) r.get("visibility");
            obj.put("visibility", VISIBILITY);
            return obj.toJSONString();
        }
        return "";
    }


    JSONArray _getcity() {
        String sql =
                "SELECT a.*, cityinfo.* FROM city_aws_hour a, ( SELECT CITY_CODE, MAX(time_code) val FROM city_aws_hour GROUP BY CITY_CODE ) b, cityinfo "
                        + "WHERE a.CITY_CODE = b.CITY_CODE AND a.time_code = b.val AND a.CITY_CODE = cityinfo.CITYCODE";

        List<Map<String, Object>> rows = hbJdbcTemplate.queryForList(sql);
        Iterator it = rows.iterator();

        JSONArray arr = new JSONArray();
        while (it.hasNext()) {
            Map<String, Object> r = (Map<String, Object>) it.next();
            String cityname = (String) r.get("CITYNAME");
            String citycode = (String) r.get("CITY_CODE");
            Timestamp ts = (Timestamp) r.get("TIME_CODE");
            String timecode = ts.toString();

            float WINDDIRECT = (float) r.get("WINDDIRECT");
            float VISIBILITY = (float) r.get("VISIBILITY");
            float DEWTEMP = (float) r.get("DEWTEMP");
            float RELHUMIDITY = (float) r.get("RELHUMIDITY");
            float WINDVELOCITY = (float) r.get("WINDVELOCITY");
            float DRYBULTEMP = (float) r.get("DRYBULTEMP");
            float STATIONPRESS = (float) r.get("STATIONPRESS");
            float PRECIPITATION = (float) r.get("PRECIPITATION");
            double lon = (double) r.get("LON");
            double lat = (double) r.get("LAT");

            JSONObject obj = new JSONObject();
            obj.put("name", cityname);//城市名称
            obj.put("code", citycode);//城市代码
            obj.put("time", timecode);//时间
            obj.put("rect", WINDDIRECT);//风向
            obj.put("lity", VISIBILITY);//能见度
            obj.put("temp", DEWTEMP);//湿度
            obj.put("dity", RELHUMIDITY);//相对湿度
            obj.put("city", WINDVELOCITY);//风速
            obj.put("ltemp", DRYBULTEMP);//空气温度
            obj.put("press", STATIONPRESS);//气压
            obj.put("ation", PRECIPITATION);//降水量
            obj.put("lon", lon);//X
            obj.put("lat", lat);//Y

            arr.add(obj);
        }
        return arr;
    }


    public Map<String, Object> findAwsDayWindByCityCode(Map<String, Object> map) {

        if (map.get("date") == "kong" || map.get("date").equals("kong")) {
            return awsdao.findAwsDayWindByCityCode_DateKong(map);
        } else {
            return awsdao.findAwsDayWindByCityCode(map);
        }


    }

    public Map<String, Object> findAwsDayWindByCityCode1(Map<String, Object> map) {

        return awsdao.findAwsDayWindByCityCode1(map);
    }
}



